Skip to main content

Go Client API Guide

The Dorcha Go Client API provides a simple and secure way to integrate with the Dorcha Gateway from Go applications. This quickstart sets up dorcha-gateway between your enterprise application and Google's Gemini models.

1. Create a Dorcha Client

// Create a new client
dorchaClient := dorcha.NewClient(
&http.Client{Timeout: 30 * time.Second},
"your-dorcha-secret-key", // Dorcha secret key
"your-entity-id", // Your internal entity ID
"gemini", // Target AI service ID
"gemini", // AI service type
)

2. Make Authenticated Requests

// Create your HTTP request as usual
req, _ := http.NewRequest("POST", "http://localhost:3128/v1beta/models/gemini-2.0-flash:generateContent", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")

// The Client API automatically signs and sends the request
resp, err := dorchaClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

The Client API automatically adds the dorcha-gateway's security headers to your requests.

Complete Example

Here's a complete example showing how to use the Go Client API with Google's Gemini models:

func main() {
// Create Dorcha client
dorchaClient := dorcha.NewClient(
&http.Client{Timeout: 30 * time.Second},
"your-dorcha-secret",
"my-app",
"gemini",
"gemini",
)

// Prompt for Gemini
jsonData := `{
"contents": [
{
"parts": [
{"text": "Hello, how are you?"}
]
}
]
}`

// Create HTTP request for Gemini
req, _ := http.NewRequest("POST", "http://localhost:3128/v1beta/models/gemini-2.0-flash:generateContent", bytes.NewBuffer([]byte(jsonData)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-goog-api-key", "your-gemini-api-key")

// Send via Dorcha (automatically signed)
resp, err := dorchaClient.Do(req)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer resp.Body.Close()

// Handle response
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Status: %s\n", resp.Status)
fmt.Printf("Response: %s\n", string(body))
}